package com.erp.pro.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.erp.common.utils.*;
import com.erp.pro.domain.InstockDO;
import com.erp.pro.domain.SupplierLogDO;
import com.erp.pro.service.SupplierLogService;
import com.erp.pur.domain.GroupDO;
import com.erp.pur.service.GroupService;
import com.erp.system.domain.UserDO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.erp.pro.domain.SupplierDO;
import com.erp.pro.service.SupplierService;

import javax.servlet.http.HttpServletResponse;

/**
 * 供应商管理
 *
 * @author Eric
 * @email shiliang900713@163.com
 * @date 2019-06-29 17:06:25
 */

@Controller
@RequestMapping("/pro/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;

    @Autowired
    private SupplierLogService supplierLogService;
    @Autowired
    private GroupService groupService;

    @GetMapping()
    @RequiresPermissions("pro:supplier:supplier")
    String Supplier(){
        return "pro/supplier/supplier";
    }

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("pro:supplier:supplier")
    public PageUtils list(@RequestParam Map<String, Object> params){
        //查询列表数据
        Query query = new Query(params);
        List<SupplierDO> supplierList = supplierService.list(query);
        int total = supplierService.count(query);
        PageUtils pageUtils = new PageUtils(supplierList, total);
        return pageUtils;
    }

    @GetMapping("/add")
    @RequiresPermissions("pro:supplier:add")
    String add(){
        return "pro/supplier/add";
    }

    @GetMapping("/edit/{id}")
    @RequiresPermissions("pro:supplier:edit")
    String edit(@PathVariable("id") Long id,Model model){
        SupplierDO supplier = supplierService.get(id);
        model.addAttribute("supplier", supplier);
        return "pro/supplier/edit";
    }

    /**
     * 保存
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions("pro:supplier:add")
    public R save( SupplierDO supplier){
        supplier.setGmtCreate(new Date());
        supplier.setGmtModified(new Date());
        UserDO current = ShiroUtils.getUser();
        supplier.setUserIdCreate(current.getUserId());
        supplier.setUserNameCreate(current.getName());
        supplier.setUserIdModified(current.getUserId());
        supplier.setUserNameModified(current.getName());
        if(supplierService.save(supplier)>0){
            //记录日志
            SupplierLogDO supplierLogDO = new SupplierLogDO();
            supplierLogDO.setGmtCreate(new Date());
            supplierLogDO.setUserIdCreate(current.getUserId());
            supplierLogDO.setUserNameCreate(current.getName());
            supplierLogDO.setRemarks("新增供应商：["+supplier.getName()+"]");
            supplierLogDO.setSupplierId(supplier.getId());
            supplierLogDO.setSupplierName(supplier.getName());
            supplierLogService.save(supplierLogDO);
            return R.ok();
        }
        return R.error();
    }
    /**
     * 修改
     */
    @ResponseBody
    @RequestMapping("/update")
    @RequiresPermissions("pro:supplier:edit")
    public R update( SupplierDO supplier){
        SupplierDO oldsupplier = supplierService.get(supplier.getId());
        String updateDescr="";
        try{
            Map<String, String> supplierupdate = GetDifference.getObject(oldsupplier, supplier);
            for(Map.Entry<String, String> entry : supplierupdate.entrySet()){
                updateDescr = updateDescr+"修改["+entry.getKey()+"]为["+entry.getValue()+"]";
                System.out.println("修改前 :" + entry.getKey());
                System.out.println("修改后 :" + entry.getValue());
            }
        }catch (Exception e){
            updateDescr = "记录修改描述异常";
        }
        supplier.setGmtModified(new Date());
        UserDO current = ShiroUtils.getUser();
        supplier.setUserIdModified(current.getUserId());
        supplier.setUserNameModified(current.getName());
        supplierService.update(supplier);
        //记录日志
        SupplierLogDO supplierLogDO = new SupplierLogDO();
        supplierLogDO.setGmtCreate(new Date());
        supplierLogDO.setUserIdCreate(current.getUserId());
        supplierLogDO.setUserNameCreate(current.getName());
        supplierLogDO.setRemarks(updateDescr);
        supplierLogDO.setSupplierId(supplier.getId());
        supplierLogDO.setSupplierName(supplier.getName());
        supplierLogService.save(supplierLogDO);
        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping( "/remove")
    @ResponseBody
    @RequiresPermissions("pro:supplier:remove")
    public R remove( Long id){
        //记录日志
        SupplierDO oldsupplier = supplierService.get(id);
        UserDO current = ShiroUtils.getUser();
        SupplierLogDO supplierLogDO = new SupplierLogDO();
        supplierLogDO.setGmtCreate(new Date());
        supplierLogDO.setUserIdCreate(current.getUserId());
        supplierLogDO.setUserNameCreate(current.getName());
        supplierLogDO.setRemarks("删除供应商["+oldsupplier.getName()+"]");
        supplierLogDO.setSupplierId(oldsupplier.getId());
        supplierLogDO.setSupplierName(oldsupplier.getName());
        supplierLogService.save(supplierLogDO);
        if(supplierService.remove(id)>0){
            return R.ok();
        }
        return R.error();
    }

    /**
     * 删除
     */
    @PostMapping( "/batchRemove")
    @ResponseBody
    @RequiresPermissions("pro:supplier:batchRemove")
    public R remove(@RequestParam("ids[]") Long[] ids){
        supplierService.batchRemove(ids);
        return R.ok();
    }

    /**
     * 查询所有的供应商
     * @return
     */
    @ResponseBody
    @GetMapping("/supplierlist")
    public  List<SupplierDO> supplierlist(){
        //查询列表数据
        List<SupplierDO> supplierList = supplierService.list(null);
        return supplierList;
    }

    @GetMapping("/getsupplierinfo/{id}")
    String getsupplierinfo(@PathVariable("id") Long id,Model model){
        SupplierDO supplier = supplierService.get(id);
        model.addAttribute("supplier", supplier);
        //查询总采购金额 总付款金额 总开票金额
        GroupDO groupDO = groupService.getAllPriceBySupplierId(id);
        model.addAttribute("groupDO", groupDO);
        return "pro/supplier/supplierinfo";
    }


    @PostMapping("/exportData")
    @ResponseBody
    public void downloadTemplate(SupplierDO supplierDO, HttpServletResponse response){
        //ProjectDO project
        Map<String, Object> params = new HashMap<>();
        if(StringUtils.isNotBlank(supplierDO.getName())){
            params.put("name",supplierDO.getName());
        }
        if(StringUtils.isNotBlank(supplierDO.getBrands())){
            params.put("brands",supplierDO.getBrands());
        }
        List<SupplierDO> supplierDOList = supplierService.list(params);
        ExcelUtil.exportExcel(supplierDOList,"供应商列表","供应商列表",SupplierDO.class,"供应商列表.xls",response);
    }
}